package com.cdsxt.util;
 
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class PackageUtil {
	
	
	public void t1(){
		String packageName = "com.cdsxt.gen.modules";
		String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + packageName.replace(".", "\\");
		System.out.println(filePath);
	}
	public static void main(String[] args) throws Exception{
		
		
		String packageName = "com.cdsxt.gen.modules";
		String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + packageName.replace(".", "\\");
		System.out.println(filePath);
		
//		List<String> classNames = getAllClassName(packageName,"Controller");
//		for (String className : classNames) {
//			System.out.println(className);
//			Class ctCls=Class.forName(className);
////			System.out.println("所有注解："+Arrays.toString(ctCls.getAnnotations()));
//			System.out.println("指定注解："+ctCls.getAnnotation(RequestMapping.class));
//			
//		}
		
	}
	/**
	 * 找到某个包下的所有的类【包含指定类名】
	 * @param packageName
	 * @param 
	 * @return
	 */
	public static List<String> getAllClassName(String packageName,String contaisClassName) {
		String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + packageName.replace(".", "\\");
		List<String> clsNames = getClassName(filePath, null);
		Iterator<String> it = clsNames.iterator();
		while (it.hasNext()) {
			String clsName = it.next();
			String simpleClsName= clsName.substring(clsName.lastIndexOf(".")+1);
			if(!simpleClsName.contains(contaisClassName)){
				it.remove();
			}
		}
		
		return clsNames;
	}
	/**
	 * 找到某个包下的所有的类
	 * @param packageName
	 * @return
	 */
	public static List<String> getAllClassName(String packageName) {
		String filePath = ClassLoader.getSystemResource("").getPath() + packageName.replace(".", "\\");
		List<String> fileNames = getClassName(filePath, null);
		return fileNames;
	}
 
	private static List<String> getClassName(String filePath, List<String> className) {
		List<String> myClassName = new ArrayList<String>();
		File file = new File(filePath);
		File[] childFiles = file.listFiles();
		for (File childFile : childFiles) {
			if (childFile.isDirectory()) {
				myClassName.addAll(getClassName(childFile.getPath(), myClassName));
			} else {
				String childFilePath = childFile.getPath();
				childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath.lastIndexOf("."));
				childFilePath = childFilePath.replace("\\", ".");
				myClassName.add(childFilePath);
			}
		}
 
		return myClassName;
	}
}